package com.javaweb.bookstore.component;


import com.javaweb.bookstore.entity.Book;
import com.javaweb.bookstore.entity.UserInfo;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class JdbcUserManager implements IUserManager {

    private static JdbcUserManager instance;

    // 2. 私有构造函数
    private JdbcUserManager() {
        try {
            HikariConfig config = new HikariConfig();
            config.setJdbcUrl("jdbc:mysql://localhost:3306/bookstore");
            config.setUsername("root");
            config.setPassword("root");
            config.setMaximumPoolSize(20);
            config.setMinimumIdle(5);
            config.setConnectionTimeout(30000);
            config.setIdleTimeout(600000);
            config.setMaxLifetime(1800000);

            dataSource = new HikariDataSource(config);
        } catch (Exception e) {

        }
    }

    // 3. 公共静态获取实例方法
    public static synchronized JdbcUserManager getInstance() {
        if (instance == null) {
            instance = new JdbcUserManager();
        }
        return instance;
    }


    HikariDataSource dataSource;

    public void createUser(UserInfo b) {


        try {
            Connection connection = dataSource.getConnection();
            String sql1 = "INSERT INTO UserInfo (name,description, password) VALUES (?,?,?)";
            // 3. 创建Statement对象
            PreparedStatement statement = connection.prepareStatement(sql1);


            statement.setString(1, b.getName());
            statement.setString(2, b.getDescription());
            statement.setString(3, b.getPassword());
            statement.executeUpdate(sql1);

            System.out.println("数据插入成功！");

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 9. 关闭资源

        }
    }

    public void removeBook(String id) {


    }

    public UserInfo getUserByNameAndPassword(String name, String password) {
        try {
            Connection connection = dataSource.getConnection();
            String sql = "SELECT * FROM userinfo where name = ? and password = ?";
            PreparedStatement statement = connection.prepareStatement(sql);

            statement.setString(1, name);
            statement.setString(2, password);
            ResultSet resultSet = statement.executeQuery();


            System.out.println("=== 查询结果 ===");

            if (resultSet.next()) {


                String desc = resultSet.getString("description");


                UserInfo book = new UserInfo();

                book.setName(name);
                book.setDescription(desc);
                book.setPassword(password);
                return book;


                //构造Boo对象
            }
            resultSet.close();

        } catch (Exception e) {
            return null;
        }
        return null;


    }


    public List<UserInfo> getUsers(String key){
        try {
            Connection connection = dataSource.getConnection();
            ResultSet resultSet;
            if(key == null || key.equals("")) {

                Statement statement = connection.createStatement();

                String sql = "SELECT * FROM userinfo";
                resultSet = statement.executeQuery(sql);
            }else{
                String sql = "SELECT * FROM userinfo where name like ? or description like ?";
                PreparedStatement statement = connection.prepareStatement(sql);

                statement.setString(1, "%" + key + "%");
                statement.setString(2, "%" + key + "%");
                resultSet = statement.executeQuery();
            }

            System.out.println("=== 查询结果 ===");
            List<UserInfo> books = new ArrayList<>();
            while (resultSet.next()) {

                String name = resultSet.getString("name");
                String desc = resultSet.getString("description");
                String password = resultSet.getString("password");

                UserInfo book = new UserInfo();

                book.setName(name);
                book.setDescription(desc);
                book.setPassword(password);
                books.add(book);


                //构造Boo对象
            }
            resultSet.close();
            return books;
        }catch (Exception e){
            return null;
        }



    }
}
